<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    ul,
    ol {
        list-style: none;
    }

    .box {
        width: 300px;
        height: 250px;
        border: 2px solid hotpink;
        margin: 200px auto 0;
    }

    ul {
        overflow: hidden;
    }

    ul li {
        background: orange;
        width: 98px;
        height: 50px;
        border-left: 1px solid black;
        border-right: 1px solid black;
        float: left;
        font: 20px/50px "";
        text-align: center;
    }

    ul li.active {
        background-color: orangered;
    }

    ol li {
        background: skyblue;
        width: 300px;
        height: 200px;
        font: 50px/200px "";
        text-align: center;
        display: none;
    }

    ol li.active {
        display: block;
    }
</style>

<body>
    <div class="box">
        <ul>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <ol>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
        </ol>
    </div>
</body>
<script>
    //1.先获取元素
    var btns = document.querySelectorAll("ul li")
    var tabs = document.querySelectorAll("ol li")
    //2.循环遍历
    for (var i = 0; i < btns.length; i++) {
        //给按钮添加一个索引
        btns[i].setAttribute("index", i)
        //给每一个按钮添加点击事件
        btns[i].onclick = function () {
            //当点击此按钮时,先遍历,移除所有按钮和选项的active
            for (var j = 0; j < btns.length; j++) {
                btns[j].classList.remove("active")
                tabs[j].classList.remove("active")
            }
            //点击事件的this指向事件源,即:按钮
            this.classList.add("active")//因为解析过程中是先循环完成,点击事件发生时才执行内部的函数,所以要用this
            // var index = this.index - 0//因为index不是元素属性,所以不能直接.
            var index = this.getAttribute("index") - 0
            // console.log(index)
            tabs[index].className = "active"
        }
    }
</script>

</html>